package com.rainner.app.action;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.app.base.action.JsonAction;
import com.rainner.app.javabean.BookBean;
import com.rainner.app.javabean.BrrowedBookBean;
import com.rainner.app.server.BookServer;
import com.rainner.app.server.BrrowedBookServer;
import com.rainner.app.server.ReaderServer;

@Results({ @Result(name="NOBOOK",type="freemarker",location="/WEB-INF/content/public/nobook.ftl"),@Result(name = "OUTLOGIN", type = "redirect", location = "/login.html"), @Result(name = "CAR", type = "freemarker", location = "/WEB-INF/content/public/car.ftl"), @Result(name = "BRROWED", type = "freemarker", location = "/WEB-INF/content/public/brrowed.ftl") })
public class CarAction extends JsonAction {
	private static final long serialVersionUID = 1L;
	private static List<BookBean> car = new ArrayList<BookBean>();
	private String type;
	private String bookId;
	private BookServer bookServer = new BookServer();
	private ReaderServer readerServer = new ReaderServer();
	private BrrowedBookServer brrowedBookServer = new BrrowedBookServer();

	@Actions({ @Action(value = "/car"), @Action(value = "/car" + "/{type}/{bookId}"), })
	public String car() throws SQLException {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object username = session.getAttribute("username");
		if (username == null) {
			car.clear();
			return "OUTLOGIN";
		}
		if ("add".equals(type)) {
			BookBean bookBean = bookServer.bookDataByBookId(bookId);
			if(bookBean.getBookNumber() <= 0){
				map.put("username", username.toString());
				return "NOBOOK";
			}
			boolean f5 = true;
			for (BookBean itearble : car) {
				if(itearble.getBookId().equals(bookBean.getBookId())){
					f5 = false;
				}
			}
			if (f5){
				car.add(bookBean);
			}
		} else if ("delete".equals(type)) {
			for (BookBean bookBean : car) {
				if (bookBean.getBookId().equals(bookId)) {
					car.remove(bookBean);
				}
			}
		}
		map.put("username", username.toString());
		map.put("car", car);
		map.put("money", readerServer.readerMoney(username.toString()));
		return "CAR";
	}

	@Action("/brrowed")
	public String brrowed() throws SQLException {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object username = session.getAttribute("username");
		if (username == null) {
			return "OUTLOGIN";
		}
		Date date = new Date();
		String currentDate = (new SimpleDateFormat("yyyy-MM-dd")).format(date);
		for (BookBean bookBean : car) {
			if (bookBean.getBookNumber() > 0) {
				BrrowedBookBean brrowedBookBean = new BrrowedBookBean();
				brrowedBookBean.setBookID(bookBean.getBookId());
				brrowedBookBean.setBorrowedBookNumber(1);
				brrowedBookBean.setBorrowedDate(currentDate);
				brrowedBookBean.setReaderUsername(username.toString());
				brrowedBookServer.brrowedBook(brrowedBookBean);
				bookServer.subBookNumber(brrowedBookBean.getBookID(), brrowedBookBean.getBorrowedBookNumber());
			}
		}
		car.clear();
		map.put("username", username.toString());
		return "BRROWED";
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setBookId(String bookId) {
		this.bookId = bookId;
	}

}
